<?php

class Player extends Zend_Db_Table_Abstract {
    protected $_name = "players";
    private $dbAdapter;

    public function init() {
        $this->dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $this->dbAdapter->setFetchMode(Zend_Db::FETCH_OBJ);
    }

    public function GetAll() {
         try{
            $sql = 'SELECT * FROM players';
            return $this->dbAdapter->fetchAll($sql);
        } catch (Zend_Exception $e) {
            echo "Error message: " . $e->getMessage() . "\n";
            exit;
        }
    }

    public function GetPlayersByNational($national) {
        try{
            // sanitize this
            $sql = "SELECT p.*, n.name as national_name FROM players p inner join nationals n on n.id=p.national_id WHERE n.name LIKE '".$national."%'";
            return $this->dbAdapter->fetchAll($sql);
        } catch (Zend_Exception $e) {
            echo "Error message: " . $e->getMessage() . "\n";
            exit;
        }
    }

    public function GetPlayerById($id){
        try {
            return $this->dbAdapter->fetchRow($this->select()->where('id=?',$id));
        } catch (Zend_Exception $e){
            echo "Error message: " . $e->getMessage() . "\n";
            exit;
        }
    }

   
}

